package io.github.yotta.export.web.manager;

import io.github.yotta.common.api.model.ApiPageRequest;
import io.github.yotta.export.web.iface.SpringExcelDataExporter;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yotta
 * @date 2023/10/30 16:04
 */
@Component
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public class MapExcelExporterRegister implements ExcelExporterRegister {

    private final ConcurrentHashMap<String, SpringExcelDataExporter<?, ? extends ApiPageRequest>> exporterConcurrentContainer =
            new ConcurrentHashMap<>();

    @Override
    public void register(String name, SpringExcelDataExporter exporter) {
        if (exporterConcurrentContainer.containsKey(name)) {
            throw new IllegalStateException(MessageFormat.format(
                    "Except only one exporter for name {0}, but found two.", name
            ));
        }
        exporterConcurrentContainer.put(name, exporter);
    }

    @Override
    public SpringExcelDataExporter get(String name) {
        return exporterConcurrentContainer.get(name);
    }

    @Override
    public <T extends SpringExcelDataExporter> T get(String name, Class<T> exporterType) {
        return (T) exporterConcurrentContainer.get(name);
    }


}
